<?php
namespace app\index\controller\practice;

use app\common\model\admin\AccountModel;
use app\common\model\index\CategoryModel;
use app\common\model\index\NavModel;
use app\common\model\paths\EmployInfoModel;
use app\common\model\paths\EmployModel;
use app\common\model\paths\TaskChapterModel;
use app\common\model\paths\TaskModel;
use app\index\controller\BaseIndex;
use think\admin\extend\DataExtend;
use think\admin\model\SystemUser;

/**
 * 实践项目
 * @package app\index\controller\practice
 * @author wangqinghui
 * @DateTime 2024-09-12 11:18:23
 */
class Train extends BaseIndex
{
    protected $check_login = true;
    /**
     * 实训项目
     * @author wangqinghui
     * @DateTime 2024-09-10 17:07:52
     * @return void
     */
    public function index()
    {
        $this->title = lang('实践项目列表');
        // 获取导航数据
        $nav_id = (int)$this->request->get('nav_id', 0);
        $map = [
            ['status', '=', 1],// 启用
            ['id', '=', $nav_id],
        ];
        $navInfo = NavModel::mk()->where($map)->find();
        // 获取学科数据
        $map = [
            ['status', '=', 1],// 启用
        ];
        $category_list = CategoryModel::mk()->where($map)->order('sort desc,id asc')->select()->toArray();
        $category_data = DataExtend::arr2tree($category_list);
        // 获取所有子类
        $default_ct_list = [];
        if(!empty($category_data)){
            foreach ($category_data as $key => $value) {
                if(!empty($value['sub'])){
                    $default_ct_list = array_merge($default_ct_list, $value['sub']);
                }
            }
        }
        $this->fetch('index', ['nav' => $navInfo, 'category' => $category_data, 'page_size' => 16, 'default_sct' => $default_ct_list]);
    }

    /**
     * 获取项目列表
     * @author wangqinghui
     * @DateTime 2024-09-10 17:10:26
     * @return void
     */
    public function list()
    {
        // 搜索内容
        $key = trim($this->request->post('key', ''));
        // 分页
        $page = (int)$this->request->post('page', 1);
        $limit = (int)$this->request->post('limit', 16);
        // 分类
        $category_id = (int)$this->request->post('category_id', 0);
        $sub_category_id = (int)$this->request->post('sub_category_id', 0);

        // 获取实践课程数据
        $map = [
            ['status', '=', 1],// 启用
        ];
        if(!empty($key)){
            $map[] = ['title', 'like', "%{$key}%"];
        }
        if($category_id > 0){
            $map[] = ['category_id', '=', $category_id];
        }
        if($sub_category_id > 0){
            $map[] = ['sub_category_id', '=', $sub_category_id];
        }
        $total = EmployModel::mk()->where($map)->count();
        $list = EmployModel::mk()->where($map)->page($page,$limit)->order('id desc')->select()->toArray();

        // 获取课程作者信息
        $author_ids = array_column($list, 'user_id');
        $author_data = [];
        if(!empty($author_ids)){
            $map = [
                ['id',  'in', $author_ids],
                ['status', '=', 1],
                ['usertype', '=', 'teacher']
            ];
            $field = 'id,nickname,headimg';
            $author_data = SystemUser::mk()->where($map)->column($field, 'id');
        }

        // 获取课程所属平台
        $platform_ids = array_column($list, 'account_id');
        $platform_data = [];
        if(!empty($platform_ids)){
            $map = [
                ['id',  'in', $platform_ids],
                ['status', '=', 1],
                ['module', 'find in set', AccountModel::MODULE_SCHOOLS]
            ];
            $field = 'id,title';
            $platform_data = AccountModel::mk()->where($map)->column($field, 'id');
        }

        // 获取关卡总数
        $ids = array_column($list, 'id');
        $map = [
            ['app_id', 'in', $ids],
            ['status', '=', 1],// 启用
            ['pid', '=', 0],
            ['level', '=', 1] // 关卡
        ];
        $chapter_total_list = EmployInfoModel::mk()->field('app_id, count(id) as num')->where($map)->group('app_id')->select()->toArray();
        $chapter_total = array_column($chapter_total_list, 'num', 'app_id');
        foreach ($list as $key => $item){
            $list[$key]['chapter_total'] = !empty($chapter_total[$item['id']]) ? $chapter_total[$item['id']] : 0;
            $list[$key]['auther'] = !empty($author_data[$item['user_id']]['nickname']) ? $author_data[$item['user_id']]['nickname'] : '';
            $list[$key]['platform'] = !empty($platform_data[$item['account_id']]['title']) ? $platform_data[$item['account_id']]['title'] : '';
        }
        $this->success('获取成功', ['total' => $total, 'list' => $list]);
    }

    /**
     * 查看实践项目详情
     * @author wangqinghui
     * @DateTime 2024-09-10 17:37:40
     * @return void
     */
    public function detail()
    {
        $this->title = lang('实践项目详情');
        $id = (int)$this->request->get('id', 0);
        // 获取项目信息
        $map = [
            ['id', '=', $id],
            ['status', '=', 1]// 启用
        ];
        $info = EmployModel::mk()->where($map)->find();
        $map = [
            ['app_id', 'in', $id],
            ['status', '=', 1],// 启用
            ['pid', '=', 0],
            ['level', '=', 1] // 关卡
        ];
        // 获取关卡信息
        $chapter_list = EmployInfoModel::mk()->where($map)->order('id asc')->select()->toArray();
        $info['chapter_total'] = count($chapter_list) ?? 0;
        $info['gold_total'] = !empty($chapter_list) ? array_sum(array_column($chapter_list, 'gold')) : 0;
        $info['detail_list'] = DataExtend::arr2tree($chapter_list);

        // 获取项目所属课程
        $map = [
            ['employ_id', '=', $info['id']],
            ['status', '=', 1]// 启用
        ];
        $taskChapterList = TaskChapterModel::mk()->where($map)->select()->toArray();
        $task_ids = array_column($taskChapterList, 'task_id');
        $course_list = [];
        if(!empty($task_ids)){
            $map = [
                ['id',  'in', $task_ids],
                ['status', '=', 1]
            ];
            $course_list = TaskModel::mk()->where($map)->select()->toArray();
        }
        $this->fetch('detail', ['info' => $info, 'course_list' => $course_list]);
    }
}